<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://cdn.bootcss.com/jquery/2.1.2/jquery.min.js"></script>
    <script>

        $(function(){

            $("#subBtn").click(function () {

                //初始化报错信息
                $("#err_opwd").html('');
                $("#err_npwd").html('');
                $("#err_npwds").html('');

                //执行当前表单的数据验证
                var opwd, npwd, npwds, flag = true;
                opwd = $("#opwd").val();
                npwd = $("#npwd").val();
                npwds = $("#npwds").val();

                if (opwd == "") {
                    $("#err_opwd").html("提示：原始密码不能为空！");
                    flag = false;
                }
                if (npwd == "") {
                    $("#err_npwd").html("提示：原始密码不能为空！");
                    flag = false;
                }
                if (npwds == "") {
                    $("#err_npwds").html("提示：原始密码不能为空！");
                    flag = false;
                }
                //验证新密码 与 确认密码是否相同
                if (npwd != npwds) {
                    $("#err_npwds").html("提示：确认密码不一致！");
                    flag = false;
                }

                if (flag) {
                    //进行提交数据 AJAX

                    $.post('pub/ajax/changePwd.php', {opwd:opwd, npwd:npwd, npwds:npwds}, function (data) {
                        if (data.code == 200) {
                            alert('提示：登陆密码已修改成功');
                            window.location.href = 'list.php';
                            return;
                        } else if(data.code == 501) {
                            $("#err_npwds").html("提示：确认密码不一致！");
                            return;
                        } else if(data.code == 502) {
                            $("#err_opwd").html("提示：原始密码错误！");
                            return;
                        } else if(data.code == 503) {
                            alert('提示：密码修改失败');
                            return;
                        }
                    }, 'json');

                }

            });

        });

    </script>
    <style>
        .error{
            font-size: 12px;
            color: red;
        }
    </style>
    <title>修改密码-MyBlog</title>
</head>
<body>

<?php

//连接数据库
include 'inc/conn.php';

//验证用户登陆
include 'inc/checkSesion.php';

//加载页面头部
include 'inc/admin_header.php';

?>


<div>

    <form>

        <p>
            原密码：<input type="password" name="opwd" id="opwd">
            <label id="err_opwd" class="error"></label>
        </p>

        <p>
            新密码：<input type="password" name="npwd" id="npwd">
            <label id="err_npwd" class="error"></label>
        </p>

        <p>
            确认密码：<input type="password" name="npwds" id="npwds">
            <label id="err_npwds" class="error"></label>
        </p>

        <button type="button" id="subBtn">确认修改</button>


    </form>

</div>

</body>
</html>